package Presentation.action;

import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Domain.Book;
import Presentationlayout.Admin;

/**
 * This listener shows the details of a book in {@link Presentationlayout.BookDetails}
 * when the user selects a book from the table in 
 * {@link Presentationlayout.AdminSearchResults}.
 * 
 * @author Rodrigo Acuna
 *
 */
public class BookDetailsAction implements ListSelectionListener {
	private Admin admin;
	
	public BookDetailsAction(Admin admin) {
		this.admin = admin;
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// Returns the index of the first selected row
	    int selectedBook = e.getFirstIndex();
		System.out.println("book selected # " + e.getFirstIndex());
		final Book bookOf = admin.getBookList().getBook(selectedBook);
		
		
        Runnable updateGui = new Runnable() {
            public void run() {
                admin.goToBookDetails(bookOf);           
            }
        };
        SwingUtilities.invokeLater(updateGui);
	}
	
}
